package com.xs.smc.bad;

import com.xs.smc.entity.Order;
import com.xs.smc.enums.OrderStatus;
import org.springframework.stereotype.Service;

@Service("bad_OrderService")
public class OrderService {

    // 商家接单
    public void confirm(Order order) {
        if (order.getStatus() == OrderStatus.PENDING_CONFIRM) {
            order.setStatus(OrderStatus.CONFIRMED);
            System.out.println("接单成功！");
        } else {
            throw new IllegalStateException("只有待接单状态才能接单！");
        }
    }

    // 商店制作：
    public void prepare(Order order) {
        if (order.getStatus() == OrderStatus.CONFIRMED) {
            order.setStatus(OrderStatus.PREPARING);
            System.out.println("开始制作！");
        } else {
            throw new IllegalStateException("只有已接单状态才能开始制作！");
        }
    }

    // 外卖员配送
    public void deliver(Order order) {
        if (order.getStatus() == OrderStatus.PREPARING) {
            order.setStatus(OrderStatus.DELIVERING);
            System.out.println("开始配送！");
        } else {
            throw new IllegalStateException("只有制作中订单才能配送！");
        }
    }

    // 完成
    public void complete(Order order) {
        if (order.getStatus() == OrderStatus.DELIVERING) {
            order.setStatus(OrderStatus.COMPLETED);
            System.out.println("订单已完成！");
        } else {
            throw new IllegalStateException("只有配送中订单才能完成！");
        }
    }

    // 取消
    public void cancel(Order order) {
        if (order.getStatus() != OrderStatus.COMPLETED && order.getStatus() != OrderStatus.CANCELLED) {
            order.setStatus(OrderStatus.CANCELLED);
            System.out.println("订单已取消！");
        } else {
            throw new IllegalStateException("已完成或已取消的订单无法取消！");
        }
    }
}